import { defineStore } from "pinia";
import { store } from "../index";

export interface IUserState {
	// 用户id
	id: number;
	// 用户名
	name: string;
	// 用户权限角色
	roles: string[];
	// 头像
	photo: string;
	menus: any[];
}

export const useUserStore = defineStore("useUserStore", {
	state: (): IUserState => {
		return {
			id: 0,
			name: "",
			photo: "",
			roles: ["admin"],
			menus: [],
		};
	},

	getters: {
		// 是否登录
		isLogin({ id }) {
			return id > 0;
		},
	},

	actions: {
		/**
		 * @description: 获取用户信息
		 * @returns result code 0 已登录并且获取用户信息成功,1 未登录, 2 后端返回的其他错误，999 服务器错误
		 */
		getInfo() {
			return new Promise<void>((resolve, reject) => {
				try {
					const result: any = { code: 0, msg: "" };
					resolve(result);
				} catch (error) {
					reject(error);
				}
			});
		},

		/**
		 * @description: 重置用户信息
		 */
		reset() {
			this.id = 0;
			this.name = "";
			this.roles = ["admin"];
			this.menus = [];
		},
		set(user: IUserState) {
			this.id = user.id;
			this.name = user.name;
			this.photo = user.photo;
			this.roles = user.roles;
			// this.roles = ["admin"];
			this.menus = [];
		},
	},

	persist: true,
});

export const useUserStoreWithOut = () => {
	return useUserStore(store);
};
